<?php

//
// +------------------------------------------------------------------------+
// | PHP Version 5.4                                                        |
// +------------------------------------------------------------------------+
// | Copyright (c) All rights reserved.                                     |
// +------------------------------------------------------------------------+
// | 世纪车来车往(北京）网络科技有限公司 版权所有！                         |
// | TEL:010-63732365  HOTLINE:400-6688-365  EMAIL:clcw@clcw.com.cn         |
// | $Revision: 1.0 $                                                       |
// +------------------------------------------------------------------------+
// | Author: luchao@clcw.com.cn                                             |
// +------------------------------------------------------------------------+
//
//应用管理
namespace AuctionBack\Controller;

use \AuctionCore\System\AuctionException;
use \AuctionCore\Common\Util;

class AppPublishController extends BaseController {

    public function __construct() {
        parent::__construct();
        $this->emp_id = $this->user_info['emp_id'];
        //模板赋值
        $this->assign("emp_id", $this->emp_id);

    }

    public function index() {
        $driver = C('DRIVER');
        $sort_id = C('SORT_ID');
        $this->assign("driver", $driver);
        $this->assign("sort_id", $sort_id);
        $this->display();
    }

    public function app_list(){
        $page = I("get.p", 1);
        $size = 10;
        $params = array();
        $params['sort_id'] = I('get.sort_id');
        $params['driver'] = I('get.driver');
        $params['status'] = I('get.status');
        //获取list
        $result = $this->get_list($page, $size, $params);
        $Page = new \AuctionCore\Common\AjaxPage($result['total'], $size, "get_page");
        $Page->setConfig('theme', " %totalRow% %header% %nowPage%/%totalPage% 页 %first% %upPage% %prePage% %linkPage% %downPage% %nextPage% %end% %jumpform%");
        $show = $Page->show();   // 分页显示输出
        //模板赋值
        $driver = C('DRIVER');
        $sort_id = C('SORT_ID');
        $this->assign("driver", $driver);
        $this->assign("sort_id", $sort_id);
        $this->assign("p", $page);  
        $this->assign('page', $show);   // 赋值分页输出
        $this->assign('lists', $result['list']);
        $this->display();
    }
    //添加页面
    public function add() {
        $driver = C('DRIVER');
        $sort_id = C('SORT_ID');
        $this->assign("driver", $driver);
        $this->assign("sort_id", $sort_id);
        $this->display();
    }
    //修改页面
    public function edit(){
        $id = I('get.id');
        if ($id < 1) {
            $this->helpJsonResult(415, "应用id不存在");
        }
        $one_client_app = M('client_app')->where(array('id'=>$id))->find();
        $driver = C('DRIVER');
        $sort_id = C('SORT_ID');
        $this->assign("driver", $driver);
        $this->assign("sort_id", $sort_id);
        $this->assign("one_client_app", $one_client_app);
        $this->display();
    }
    //修改提交
    public function edit_post(){
        $id = I('post.app_id');
        if ($id < 1) {
            $this->helpJsonResult(415, "应用id不存在");
        }
        $sort_id = I('post.sort_id');
        if (empty($sort_id)) {
            $this->helpJsonResult(411, "应用分类不能为空");
        }
        $driver = I('post.driver');
        if (empty($driver)) {
            $this->helpJsonResult(412, "应用设备不能为空");
        }
        $appname = I('post.appname');
        if (empty($appname)) {
            $this->helpJsonResult(413, "应用名称不能为空");
        }
        $version = I('post.version');
        if (empty($version)) {
            $this->helpJsonResult(414, "应用版本不能为空");
        }
        $force = I('post.force');
        if ($force == '') {
            $this->helpJsonResult(415, "请选择是否强制更新");
        }
        if ($driver == 1 || $driver == 2) {
            $main_path = "app/android";
        }elseif ($driver == 3 || $driver == 4) {
            $main_path = "app/ios";
        }
        if ($sort_id == 1) {
            $sort_path = "return";
        }elseif ($sort_id == 2) {
            $sort_path = "lpaicheapp";
        }elseif ($sort_id == 3) {
            $sort_path = "clcwapp";
        }
        $apppackage = I('data.apppackage', '', '', $_FILES);
        $remark = I('post.remark');
        if (!empty($apppackage)){
            $ext = get_extension($apppackage['name']);
            $exts = array('apk', 'APK');
             if (!in_array($ext, $exts)) {
                $this->helpJsonResult(417, "上传文件格式不正确");
            }
            $object =  $apppackage['name'];
            $tmp_path = $apppackage['tmp_name'];
            try {
               $aliObject = \AuctionCore\Common\Image::upload_package_by_file(OSS_BUCKET, $object, $tmp_path,null,'',$main_path.$sort_path);
                if ($aliObject) {
                    $host = get_big_content_cdnurl();
                    $url = $host . $aliObject;
                    $data['link'] = $url;
                    $data['sort_id'] = $sort_id;
                    $data['driver'] = $driver;
                    $data['name'] = $appname;
                    $data['version'] = $version;
                    $data['note'] = $remark;
                    $data['force'] = $force;
                    $data['modifytime'] = date('Y-m-d H:i:s');
                    $edit_id = M('client_app')->where(array('id'=>$id))->save($data);
                };
                echo Util::auto_jsonp_or_json($_REQUEST,array('code'=>200,'msg'=>'修改成功','data'=>$edit_id));
                exit;
            } catch (AuctionException $e) {
                echo Util::auto_jsonp_or_json($_REQUEST,array('msg'=>$e->getMessage(),'code'=>$e->getCode()));
                exit;
            }
        }else{
            $data['sort_id'] = $sort_id;
            $data['driver'] = $driver;
            $data['name'] = $appname;
            $data['version'] = $version;
            $data['note'] = $remark;
            $data['force'] = $force;
            $data['modifytime'] = date('Y-m-d H:i:s');
            $edit_id = M('client_app')->where(array('id'=>$id))->save($data);
            echo Util::auto_jsonp_or_json($_REQUEST,array('code'=>200,'msg'=>'修改成功','data'=>$edit_id));
            exit;
        }
    }
    //添加提交
    public function add_post() {
        $sort_id = I('post.sort_id');
        if (empty($sort_id)) {
            $this->helpJsonResult(411, "应用分类不能为空");
        }
        $driver = I('post.driver');
        if (empty($driver)) {
            $this->helpJsonResult(412, "应用设备不能为空");
        }
        $appname = I('post.appname');
        if (empty($appname)) {
            $this->helpJsonResult(413, "应用名称不能为空");
        }
        $version = I('post.version');
        if (empty($version)) {
            $this->helpJsonResult(414, "应用版本不能为空");
        }
        $force = I('post.force');
        if ($force == '') {
            $this->helpJsonResult(415, "请选择是否强制更新");
        }
        if ($driver == 1 || $driver == 2) {
            $main_path = "app/android/";
        }elseif ($driver == 3 || $driver == 4) {
            $main_path = "app/ios/";
        }
        if ($sort_id == 1) {
            $sort_path = "return";
        }elseif ($sort_id == 2) {
            $sort_path = "lpaicheapp";
        }elseif ($sort_id == 3) {
            $sort_path = "clcwapp";
        }
        $remark = I('post.remark');
        $apppackage = I('data.apppackage', '', '', $_FILES);
        if (empty($apppackage)) {
            $this->helpJsonResult(416, "上传不能为空");
        }
        $ext = get_extension($apppackage['name']);
        $exts = array('apk', 'APK');
         if (!in_array($ext, $exts)) {
            $this->helpJsonResult(417, "上传文件格式不正确");
        }
        if (!empty($apppackage)){
            $object =  $apppackage['name'];
            $tmp_path = $apppackage['tmp_name'];
            $map = array();
            $map['link'] = array('like','%'.$object.'%');
            $is_exit = M('client_app')->where($map)->find();
            if ($is_exit) {
                $this->helpJsonResult(418, "存在同名app包，请修改后重新上传");
            }
            try {
               $aliObject = \AuctionCore\Common\Image::upload_package_by_file(OSS_BUCKET, $object, $tmp_path,null,'',$main_path.$sort_path);
                if ($aliObject) {
                    $host = get_big_content_cdnurl();
                    $url = $host . $aliObject;
                    $data['link'] = $url;
                    $data['sort_id'] = $sort_id;
                    $data['driver'] = $driver;
                    $data['name'] = $appname;
                    $data['version'] = $version;
                    $data['sort'] = $sort;
                    $data['note'] = $remark;
                    $data['force'] = $force;
                    $data['status'] = 0;
                    $data['created_at'] = date('Y-m-d H:i:s');
                    $id = M('client_app')->data($data)->add();
                };
                echo Util::auto_jsonp_or_json($_REQUEST,array('code'=>200,'msg'=>'添加成功','data'=>$id));
                exit;
            } catch (AuctionException $e) {
                echo Util::auto_jsonp_or_json($_REQUEST,array('msg'=>$e->getMessage(),'code'=>$e->getCode()));
                exit;
            }
        }

    }

    public function get_list($page = 1, $size = 10, $paramer) {
        $map = array();
        if ($paramer['sort_id']) {
            $map['sort_id'] = array('eq', $paramer['sort_id']);
        }
        if ($paramer['driver']) {
            $map['driver'] = array('eq', $paramer['driver']);
        }
        if ($paramer['status'] !== "") {
            $map['status'] = array('eq', $paramer['status']);
        }
        $total = M('client_app')->where($map)->count();
        $list = M('client_app')->where($map)->limit($size)->page($page)->order("id desc")->select();
        $result = array(
            'list' => $list,
            'total' => $total
        );
        return $result;
    }

    //修改状态
    public function ci_op(){
        try{
            $id = I('post.id');
            $op_type = I('post.op_type');
            if($op_type=='1'){
                M('client_app')->where(array('id' => $id))->save(array('status' => '1'));
            }else if($op_type==='0'){
                M('client_app')->where(array('id' => $id))->save(array('status' => '0'));
            }else{
                throw new AuctionException('操作非法！');
            }

            echo Util::auto_jsonp_or_json($_REQUEST,array('code'=>200,'msg'=>'','data'=>''));
            exit;
        }catch(AuctionException $e){
            echo Util::auto_jsonp_or_json($_REQUEST,array('msg'=>$e->getMessage(),'code'=>$e->getCode()));
            exit;
        }
    }
}